package com.springboot.config;

import demo.common.util.PrintUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * BeanPostProcessor: 后置处理器，说明详见原生 spring 篇。
 *
 * @author yexi
 */
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.printf("名称为%s、类型为%s的bean即将调用初始化方法\n", beanName, bean.getClass());
        // 将bean返回到容器中
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.printf("名称为%s、类型为%s的bean初始化成功\n", beanName, bean.getClass());
        PrintUtils.println();
        return bean;
    }
}
